package com.uwang.library.config;

import java.util.List;
import java.util.TimerTask;

import javax.servlet.ServletContext;

import com.uwang.library.dao.MyTimerTaskDao;
import com.uwang.library.dao.impl.MyTimerTaskDaoImpl;
import com.uwang.library.model.BeOverdueModel;
import com.uwang.library.service.MyTimerTaskService;
import com.uwang.library.service.impl.MyTimerTaskServiceImpl;
import com.uwang.library.util.MessageMultiXsend;

public class MyTimerTask extends TimerTask {
	private static boolean isRunning = false;
	private ServletContext context = null;

	public MyTimerTask(ServletContext context) {
		this.context = context;
	}

	@Override
	public void run() {
		MyTimerTaskService myTimerTaskService = new MyTimerTaskServiceImpl();
		MessageMultiXsend messageMultiXsend = new MessageMultiXsend();
		MyTimerTaskDao myTimerTaskDao = new MyTimerTaskDaoImpl();
		
		
		// 手机号 , 用户名 , 借书时间, 书籍名称, 还书时间
		String to = "13983500679";
		String name = "涛哥";
		String borrowingTime = "1号";
		String book = "红楼梦";
		String term = "5号";
		if (!isRunning) {
			isRunning = true;
			// 逾期用户发送短信提醒
			List<BeOverdueModel> beOverdue = myTimerTaskService.getBeOverdue();
			if(beOverdue.size() != 0) {
					for(int i = 0;i < beOverdue.size();i++) {
						if( beOverdue.get(i).getStatus()== 1) {
						//发送短信
						System.out.println("发送短信");
						to =beOverdue.get(i).getUserPhone();
						name =beOverdue.get(i).getUserName();
						borrowingTime =beOverdue.get(i).getBorrowingTime().toString();
						book =beOverdue.get(i).getBookName();
						term =beOverdue.get(i).getTerm().toString();
						messageMultiXsend.mmmm(to,name,borrowingTime,book,term);
						System.out.println("发送短信给"+name);
						System.out.println("尊敬的"+name+"您好！您于"+borrowingTime+"在XX图书馆借阅书籍《"+book+"》未在规定时间内("+term+")归还,为了本馆的正常营业,请尽快归还,谢谢");
						int id =beOverdue.get(i).getId();
						int status = (beOverdue.get(i).getStatus())+1;
						myTimerTaskDao.setstatus(id, status);
						}
					}
			}else {
			}
			System.out.println("监听器运行中");
			
			
			// 手机号 , 用户名 , 借书时间, 书籍名称, 还书时间
			
			
			// 发送邮箱提醒
			isRunning = false;
		} else {
			context.log("上一次任务执行还未结束");
		}

	}

}
